import axios from 'axios';
import qs from 'qs';
import { requestHeader } from './params';
import { responseHandler, responseHandler_face } from './response';
import { encrypt, getUrlParam ,encrypt_cydn} from './des';
import store from "@/stores";
import { sendParam } from "./rsa";

/**
 * 请求头参数规则 token + client + version + language + timezone
 */
function getAuthenticator () {
  // const token = Cookies.get('ovo_ticket');
  return `${token} ${requestHeader.CLIENT_FLAG} ${requestHeader.version} ${requestHeader.LANGUAGE} ${requestHeader.TIMEZONE_FLAG}`;
}

axios.defaults.timeout = 65000; // 超时时间65s

/**
 * 普通get请求
 */
export const get = async function (url, params = {}, needSecret = false) {
  let res = await axios.get(url, {
    params,
    headers: {
      token: getUrlParam('token') || 'cxgc123456'
    }
  });
  return responseHandler(res, needSecret);
};

/**
 * 普通post请求
 */
export const post = async function (url, params = {}) {
  let res = await axios.post(url, qs.stringify(params), {
    headers: {
      token: getUrlParam('token') || 'cxgc123456'
    }
  });
  return responseHandler(res);
};

/**
 * json格式的Post请求
 */
export const postJson = async function (url, params = {}, needSecret) {
  let requestObj = params;
  if (needSecret) {
    requestObj = encrypt(JSON.stringify(params), '98fbffd1064a4355b8abaacb6fa96f94');
  }
  let res = await axios.post(url, requestObj, {
    headers: {
      // token: getUrlParam('token') || '372d2606-000b-4986-82b2-0658878fc362'
      // token: getUrlParam('token') || '79cd1f04-1920-48e0-9cc0-943b3f163971' //史泰token
      token: getUrlParam('token') || 'cxgc123456' //林明伟token
      // token: getUrlParam('token') || 'e3e23bbe-d701-4c3c-ba49-17642c081db8'
      // token: ''
    }
  });
  return responseHandler(res, needSecret);
};
/**
 * json格式的Post请求 cydn
 */
export const postJson_cydn = async function (url, params = {}, needSecret) {
  let requestObj = params;
  if (needSecret) {
    requestObj = encrypt_cydn(JSON.stringify(params), '98fbffd1064a4355b8abaacb6fa96f94');
  }
  let res = await axios.post(url, requestObj, {
    headers: {
      // token: getUrlParam('token') || '372d2606-000b-4986-82b2-0658878fc362'
      // token: getUrlParam('token') || '79cd1f04-1920-48e0-9cc0-943b3f163971' //史泰token
      token: 'cxgc123456', // 临时的token
      requestType:'post',
      // token: getUrlParam('token') || 'e3e23bbe-d701-4c3c-ba49-17642c081db8'
      // token: ''
      Authorization:'Bearer ' + store.state.user.token_cydn 
    }
  });
  return responseHandler(res, needSecret);
};

/**
 * json格式的Post请求 face
 */
export const postJson_face = async function (url, params = {}, needSecret) {
  let requestObj = params;
  if (needSecret) {
    requestObj = sendParam(requestObj)
  }
  console.log(requestObj,'掉接口了')
  let res = await axios.post(url, requestObj, {
    headers: {
    }
  });
  return responseHandler_face(res, needSecret);
};

/**
 * postBody请求
 * @param url
 * @param params
 * @returns {*}
 */
export const postRaw = async function (url, params = {}, bodyParams = []) {
  let res = await axios.post(url + '?' + qs.stringify(params), bodyParams, {
    headers: {
      // 'Ovo-Authorization': getAuthenticator()
    }
  });
  return responseHandler(res);
};

export const postFile = async function (url, formdata, needSecret) {
  let res = await axios({
    url: url,
    method: 'post',
    data: formdata,
    headers: {
      // 'Content-Type': 'application/json;charset=UTF-8',
      "token": getUrlParam('token') || 'cxgc123456',
    }
  });
  return responseHandler(res, needSecret);
};
// 镇易融
export const postJson_face_zyr = async function (url, params = {}, needSecret) {
  let requestObj = params;
  // if (needSecret) {
  //   requestObj = sendParam(requestObj)
  // }
  console.log(requestObj,'掉接口了')
  let res = await axios.post(url, requestObj, {
    headers: {
    }
  });
  return responseHandler_face(res, needSecret);
};